import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-05-08
 * Time: 17:43
 */
public class Homework {
    /**
     * 二分查找
     */
    public static int find(int[] array, int key) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = (left+right) / 2;//注意：(mid)值要随着(left)和(right)改变，要写成这里的局部变量
            if ( array[mid] == key) {
                return mid;
            }else if (key > array[mid]) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        System.out.println(find(array, 4));
    }













    /**
     *奇数位于偶数之前
     *
     */
    public static void main2(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6};
        int left = 0;
        int right = array.length-1;
        int[] array2 = new int[array.length];
        for (int i:array) {
            if (i % 2 != 0) {
                array2[left] = i;
                left++;
            }else {
                array2[right] = i;
                right--;
            }
        }
        System.out.println(Arrays.toString(array2));
    }








    /**
     * 改变原有数组元素的值
     * @param array
     * @return
     */
    public static int[] transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] *= 2;
        }
        return array;
    }
    public static void main1(String[] args) {
        int[] array = new int[]{1,2,3};
        int[] ret = transform(array);
        System.out.println(Arrays.toString(ret));
    }
}
